package test.gps;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Environment;

public class FileHelper {
	private Context context;
	private boolean hasSD = false;
	private String SDPATH;
	private String FILESPATH;

	public FileHelper(Context context) {
		this.context = context;
		hasSD = Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED);
		SDPATH = Environment.getExternalStorageDirectory().getPath();
		FILESPATH = this.context.getFilesDir().getPath();
	}

	/**
	 * 
	 * @throws IOException
	 */
	public File createSDFile(String FolderName) throws IOException {
		File file1 = new File("sdcard/GPS");
		if (!file1.exists()) {
			file1.mkdirs();
		}
		File file2 = new File("sdcard/GPS/"+FolderName);
		if (!file2.exists()) {
			file2.mkdirs();
		}
		// File file = new File("sdcard/GPS" + "/" + fileName);
		// if (!file.exists()) {
		// file.createNewFile();
		// }
		return file2;
	}

	/**
	 * 
	 * @param fileName
	 */
	public boolean deleteSDFile(String FolderName) {
		File file = new File("sdcard/GPS" + "/" + FolderName);
		if (file == null || !file.exists() || file.isDirectory())
			return false;
		return file.delete();
	}

	/** 
*/
	public void writeSDFile(String str, String FolderName, String fileName) {
		try {
			// FileWriter fw = new FileWriter("sdcard/GPS/" + fileName,true);
			File f = new File("sdcard/GPS/" +FolderName+ "/" + fileName);
			// fw.write(str);
			// fw.write("\n");
			FileOutputStream outputStream = new FileOutputStream(f, true);
			outputStream.write(str.getBytes());

			outputStream.close();
			// FileOutputStream os = new FileOutputStream(f);
			//
			// DataOutputStream out = new DataOutputStream(os);
			// out.writeShort(2);
			// out.writeUTF("");
			// System.out.println(out);
			// fw.flush();
			// fw.close();
			// System.out.println(fw);
		} catch (Exception e) {
		}
	}

	/**
	 * 
	 * @param fileName
	 * @return
	 */
	public String readSDFile(String FolderName,String fileName) {
		StringBuffer sb = new StringBuffer();
		File file = new File("sdcard/GPS/" +FolderName+ "/" + fileName);
		try {
			FileInputStream fis = new FileInputStream(file);
			int c;
			while ((c = fis.read()) != -1) {
				sb.append((char) c);
			}
			fis.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}

	public List<String> getFileName(String FolderName) {

		// 图片列表
		List<String> picList = new ArrayList<String>();

		// 得到sd卡内路径
		String Path = "sdcard/GPS/"+ FolderName;

		// 得到该路径文件夹下所有的文件
		File mfile = new File(Path);
		File[] files = mfile.listFiles();
		for (int i = 0; i < files.length; i++) {
			File file = files[i];
			picList.add(file.getName());
		}
		return picList;

	}
	public List<String> getFolderName() {

		// 图片列表
		List<String> picList = new ArrayList<String>();

		// 得到sd卡内路径
		String Path = "sdcard/GPS";

		// 得到该路径文件夹下所有的文件
		File mfile = new File(Path);
		File[] files = mfile.listFiles();
		for (int i = 0; i < files.length; i++) {
			File file = files[i];
			picList.add(file.getName());
		}
		return picList;

	}

	public String getFILESPATH() {
		return FILESPATH;
	}

	public String getSDPATH() {
		return SDPATH;
	}

	public boolean hasSD() {
		return hasSD;
	}

}